package org.minproject.my_library_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.minproject.my_library_back.common.CommonResponse;
import org.minproject.my_library_back.domain.Comment;
import org.minproject.my_library_back.domain.User;
import org.minproject.my_library_back.service.CommentService;
import org.minproject.my_library_back.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Lenovo
* @description 针对表【comment】的数据库操作Service实现
* @createDate 2024-11-21 09:11:46
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public CommonResponse<List<Comment>> getAllComments() throws Exception {
        List<Comment> comments = commentMapper.selectList(null);

        return CommonResponse.createForSuccess("查询成功",comments);
    }

    @Override
    public CommonResponse<List<Comment>> getCommentById(int bookid) throws Exception {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("book_id", bookid);
        List<Comment> comments=commentMapper.selectList(queryWrapper);
        return CommonResponse.createForSuccess("查询成功",comments);
    }

    @Override
    public CommonResponse<Comment> addComment(Comment comment) throws Exception {
        commentMapper.insert(comment);
        return CommonResponse.createForSuccess("添加成功",comment);
    }

    @Override
    public CommonResponse<Comment> deleteComment(int commentid) throws Exception {
        Comment comment = commentMapper.selectById(commentid);
        commentMapper.deleteById(commentid);
        return CommonResponse.createForSuccess("删除成功",comment);
    }
}




